<?php

class SiteController extends Controller
{
    private $isActive;
    private $viewData;
    private $modelCategory;
    private $modelItem;
    private $modelSalePoint;

//    public function init() {
//
//        $this->modelItem = new ItemForm();
//        
//        $this->modelSalePoint = new SalePointModel();
//        
//        $this->modelCategory = new CategoryForm();
//    }
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{           
		$this->render('index');
	}
        public function actionSalePoint()
	{
//            $model_sale_point = new SalePointModel();
            $this->viewData['str_type_business'] =  $this->actionGetTypeBusiness(); 
                        if ($this->checkLoginAdmin()) {
                        $this->render('salepoint',  $this->viewData);
                        }
	}
        public function actionSalePointEdit() {
                        $model = new SalePointModel();
                        $modelTypeBusiness = new TypeBusinessModel();
                        if (isset($_POST['SalePointModel'])) {
                            $model->attributes = $_POST['SalePointModel'];
                            //elfinder
                            $image = $_POST['SalePointModel']['sale_image'];
                            $part = explode('/', $image);
                            $model->sale_image = $part[count($part)-1];
                            
                             if (isset($model->sale_area)) {
                                if ($model->sale_area == '1') {
                                    $model->sale_area = $model->arrArea['0'];
                                    } elseif ($model->sale_area == '2') {
                                        $model->sale_area = $model->arrArea['1'];
                                        }            
                                        }       
                            if($model->edit()) 
                                $this->redirect('../site/salepoint');
                        }
                        if (isset($_GET['id']) && $_GET['id'] != null) {
                            $this->viewData['row_id'] = $_GET['id'];
                        }
//                        else {
//                            $this->viewData['row_id'] = 
//                        }
                        $this->viewData['model'] = $model;
                        $this->viewData['rows_salepoint'] = $model->getSalePoint($this->viewData['row_id']);
                        $this->viewData['data_business'] = $modelTypeBusiness->getDatabase();

                        if ($this->checkLoginAdmin())
                        $this->render('salepointedit',  $this->viewData);
                    }
                    
        

        /**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
		if($error=Yii::app()->errorHandler->error)
		{
			if(Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else
				$this->render('error', $error);
		}
	}

	/**
	 * Displays the contact page
	 */
	public function actionContact()
	{
		$model=new ContactForm;
		if(isset($_POST['ContactForm']))
		{
			$model->attributes=$_POST['ContactForm'];
			if($model->validate())
			{
				$name='=?UTF-8?B?'.base64_encode($model->name).'?=';
				$subject='=?UTF-8?B?'.base64_encode($model->subject).'?=';
				$headers="From: $name <{$model->email}>\r\n".
					"Reply-To: {$model->email}\r\n".
					"MIME-Version: 1.0\r\n".
					"Content-Type: text/plain; charset=UTF-8";

				mail(Yii::app()->params['adminEmail'],$subject,$model->body,$headers);
				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
				$this->refresh();
			}
		}
		$this->render('contact',array('model'=>$model));
	}

	/**
	 * Displays the login page
	 */
	public function actionLogin()
	{
		$model=new LoginModel;
		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		// collect user input data
		if(isset($_POST['LoginModel']))
		{
			$model->attributes=$_POST['LoginModel'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login()) {
                                                                $this->redirect(Yii::app()->baseUrl."/index.php/site/salepoint");      
                                                            }                                                   
                                        }
                                            
                if (isset($_POST['ForgotModel'])) {
                //forgot form by dung3625@gmail.com
                $model = new ForgotModel;
                if(isset($_POST['ajax']) && $_POST['ajax']==='forgot-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
                    
		// collect user input data
		if(isset($_POST['ForgotModel']))
		{
			$model->attributes=$_POST['ForgotModel'];
			// validate user input and redirect to the previous page if valid
                                                          if($model->validate() && $model->forgot()) {
                                                                        $this->redirect("../site/forgot");
                                                            }
		}
                }
		// display the login form
                                        $this->layout = FALSE;
		$this->render('login',array('model'=>$model));
	}

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
                                       unset(Yii::app()->session['login_admin']);
		$this->redirect(Yii::app()->homeUrl);
	}
                    private function checkLoginAdmin() {
                        if (Yii::app()->session['login_admin']) {
                            return true;
                        }
                        else {
//                            $this->layout = false;//clear layout
                                // display the login form
                            $this->redirect(Yii::app()->homeUrl."/admin");                                              
                            }
                    }
        public function actionGetlstDistricts() {
            $model_salepoint = new SalePointModel();
            $arrDistrict = array();
            if (isset($_GET['city'])) {
                $city = $_GET['city'];
                if ($city=='1') {
                    $arrDistrict = $model_salepoint->arrDistrict[0];
                } elseif ($city=='2') {
                    $arrDistrict = $model_salepoint->arrDistrict[1];
            }
            }
            $district = json_encode($arrDistrict);
            echo $district;
        }
        public function actionGetTypeBusiness() {
            $type_businness = new TypeBusinessModel;
            $data = $type_businness->getDatabase();
                        $string = array();
        foreach ($data as $key => $value) {
            $string[$value['id']] = $value['name'];
        }
            return json_encode($string);
        }
}